package com.example.seakdreamandroid.sleep;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

import com.example.seakdreamandroid.MainActivity;
import com.example.seakdreamandroid.R;
import com.example.seakdreamandroid.sleep.adapter.ScrollPickerAdapter;
import com.example.seakdreamandroid.sleep.view.ScrollPickerView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class RecordSleep extends AppCompatActivity {

    private ScrollPickerView spvHour, spvMinute;
    private ImageButton start, img_btn_return_sleep;
    private String text1, text2;
    private AlertDialog alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_sleep);

        initViews();
        initData();

        System.out.println(spvHour.getTag());

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Date date = new Date();
                String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
                Intent intent = new Intent(RecordSleep.this, StartSleep.class);
                Bundle bundle = new Bundle();
                bundle.putInt("page",1);
                bundle.putString("startTime", startTime);
                bundle.putString("hour", text1);
                bundle.putString("minute", text2);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        img_btn_return_sleep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RecordSleep.this, MainActivity.class);
                intent.putExtra("id", 1);
                startActivity(intent);
            }
        });
    }


    private void initData() {
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        String itemData;
        for (int i = 1; i < 24; i++) {
            if (i < 10) {
                itemData = "0" + i + "";
            } else {
                itemData = i + "";
            }
            list1.add(itemData);
        }
        for (int i = 1; i < 60; i++) {
            if (i < 10) {
                itemData = "0" + i + "";
            } else {
                itemData = i + "";
            }
            list2.add(itemData);
        }

        ScrollPickerAdapter.ScrollPickerAdapterBuilder<String> builder1 =
                new ScrollPickerAdapter.ScrollPickerAdapterBuilder<String>(this)
                        .selectedItemOffset(1)
                        .visibleItemNumber(3)
                        .setDivideLineColor("#E5E5E5")
                        .setItemViewProvider(null)
                        .setDataList(list1)
                        .setOnScrolledListener(new ScrollPickerAdapter.OnScrollListener() {
                            @Override
                            public void onScrolled(View currentItemView) {
                                text1 = currentItemView.getTag().toString();
                            }
                        });
        ScrollPickerAdapter.ScrollPickerAdapterBuilder<String> builder2 =
                new ScrollPickerAdapter.ScrollPickerAdapterBuilder<String>(this)
                        .selectedItemOffset(1)
                        .visibleItemNumber(3)
                        .setDivideLineColor("#E5E5E5")
                        .setItemViewProvider(null)
                        .setDataList(list2)
                        .setOnScrolledListener(new ScrollPickerAdapter.OnScrollListener() {
                            @Override
                            public void onScrolled(View currentItemView) {
                                text2 = currentItemView.getTag().toString();
                            }
                        });

        spvHour.setAdapter(builder1.build());
        spvMinute.setAdapter(builder2.build());

    }

    private void initViews() {
        spvHour = findViewById(R.id.spv_hour);
        spvMinute = findViewById(R.id.spv_minutes);
        spvHour.setLayoutManager(new LinearLayoutManager(this));
        spvMinute.setLayoutManager(new LinearLayoutManager(this));
        start = findViewById(R.id.start_sleep);
        img_btn_return_sleep = findViewById(R.id.img_btn_return_sleep);
    }
}